package com.pctx.bibaregisterlogin.controller;

import com.github.pagehelper.PageHelper;
import com.pctx.bibacommon.result.Result;
import com.pctx.bibacommon.util.DateTimeUtil;
import com.pctx.bibaregisterlogin.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("page4Company")
    public Result page4Company(@RequestParam(value = "companyUuid",required = true) String companyUuid,
                               @RequestParam(value = "username",required = false) String username,
                               @RequestParam(value = "startTime",required = false) String startTime,
                               @RequestParam(value = "endTime",required = false) String endTime,
                               @RequestParam(value = "departmentUuid",required = false) String departmentUuid,
                               @RequestParam(value = "upperUuid",required = false) String upperUuid,
                               @RequestParam(value = "page",defaultValue = "1") Integer page,
                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize)throws Exception{
        Date start = null;
        Date end = null;
        if (StringUtils.isNotEmpty(startTime))
            start = DateTimeUtil.toDate(startTime);
        if (StringUtils.isNotEmpty(endTime))
            end = DateTimeUtil.toDate(endTime);
        PageHelper.startPage(page,pageSize);
        return userService.getUser4Company(companyUuid,username,start,end,departmentUuid,upperUuid);
    }

    @RequestMapping("/addDepartment")
    public Result addDepartment(@RequestParam("departmentname") String departmentname, @RequestParam("username") String username) {
        return userService.addDepartment(departmentname, username);
    }

    @RequestMapping("/addPositionone")
    public Result addPositionone(@RequestParam("positionname") String positionname, @RequestParam("departmentuuid") String departmentuuid, @RequestParam("username") String username) {
        return userService.addPositionone(positionname, departmentuuid, username);
    }

    @RequestMapping("/selectDepartment")
    public Result selectDepartment(@RequestParam("companyUuid") String companyUuid) {
        return userService.selectDepartment(companyUuid);
    }

    @RequestMapping("/selectPosition")
    public Result selectPosition(@RequestParam("companyUuid") String companyUuid, @RequestParam("departmentuuid") String departmentuuid) {
        return userService.selectPosition(companyUuid, departmentuuid);
    }

    @RequestMapping("/addUser")
    public Result addUser(@RequestParam("username") String username, @RequestParam("name") String name, @RequestParam("mobile") String mobile, @RequestParam("mail") String mail, @RequestParam("sex") String sex,
                          @RequestParam("departmentuuid") String departmentuuid, @RequestParam("positionuuid") String positionuuid, @RequestParam("upperUuid") String upperUuid, @RequestParam("subordinate") String subordinate) {
        return userService.addUser(username, name, mobile, mail, sex, departmentuuid, positionuuid, upperUuid, subordinate);
    }

    @RequestMapping("/updateUser")
    public Result updateUser(@RequestParam("username") String username, @RequestParam("name") String name, @RequestParam("mobile") String mobile,
                             @RequestParam("mail") String mail, @RequestParam("sex") String sex, @RequestParam("departmentuuid") String departmentuuid,
                             @RequestParam("positionuuid") String positionuuid, @RequestParam("upperleaderuuid") String upperleaderuuid, @RequestParam("subordinate") String subordinate) {
        return userService.updateUser(username, name, mobile, mail, sex, departmentuuid, positionuuid, upperleaderuuid, subordinate);
    }

    @RequestMapping("/hassubordinate")
    public Result hassubordinate(@RequestParam("companyUuid") String companyUuid) {

        return userService.hassubordinate(companyUuid);
    }

    @RequestMapping("/deleteUser")
    public Result deleteUser(@RequestParam("username") String username) {
        return userService.deleteUser(username);
    }

    @RequestMapping("/resetPassword")
    public Result resetPassword(@RequestParam("username") String username) {
        return userService.resetPassword(username);
    }

    @RequestMapping("/personalMessage")
    public Result personalMessage(String username) throws Exception {
    return    userService.personalMessage(username);


    }
}